import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:dali_ng_pera/global/dnp_global_manager.dart';
import 'package:dali_ng_pera/manager/dnp_plugin_manager.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import '../home/dnp_home_tab_page.dart';
import '../order/dnp_order_list_page.dart';
import '../profile/dnp_profile_tab_page.dart';
import 'dnp_main_controller.dart';

class DnpMainPage extends StatefulWidget {
  const DnpMainPage({super.key});

  @override
  State<DnpMainPage> createState() => _DnpMainPageState();
}

class _DnpMainPageState extends State<DnpMainPage> {

  final controller = Get.put(DnpMainController());

  final List<Widget> _pages = [
    const DnpHomeTabPage(),
    const DnpOrderListPage(),
    DnpProfileTabPage(),
  ];

  @override
  void initState() {
    super.initState();
    controller.ctx = context;
    DnpGlobalManager().setBuildContext(context);

    Future.delayed(Duration(milliseconds: 300),(){
      checkHasNet();
    });
    controller.showAlertDialog();
  }

  void checkHasNet() async {
    List<ConnectivityResult> connectivityResult = await Connectivity().checkConnectivity();
    if (connectivityResult.contains(ConnectivityResult.none)) {
      DnpPluginManager().showToast("Network error. Please check if you are connected to the network?");
    }
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.light,
      child: GetBuilder<DnpMainController>(
        builder: (controller) {
          return Scaffold(
            body:_contentWidget(),
          );
        },
      ),
    );
  }

  Widget _contentWidget () {
    return Column(
      children: [
        Expanded(child: _topWidget()),
        Container(
          color: Color(0xFF1D4AC8),
          height: MediaQuery.of(context).padding.bottom + 50,
          child: _bottomWidget(),
        )
      ],
    );
  }

  Widget _topWidget() {
    return TabBarView(
      physics: const NeverScrollableScrollPhysics(),
      controller: controller.tabController,
      children: _pages,
    );
  }

  Widget _bottomWidget() {
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            _buildBottomIcon(iconName: "main_home", index: 0),
            _buildBottomIcon(iconName: "main_list", index: 1),
            _buildBottomIcon(iconName: "main_profile", index: 2),
          ],
        )
      ],
    );
  }

  Widget _buildBottomIcon({
    required String iconName,
    required int index,
  }) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () => controller.clickMainIndex(index),
      child: Container(
        width: 100,
        color: Colors.transparent,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const SizedBox(height: 12),
            _bottomIcon(iconName, index),
          ],
        ),
      ),
    );
  }

  Widget _bottomIcon(String iconName, int index) {
    final imagePath = "assets/images/$iconName"
        "${controller.selectMainIndex == index ? '_select' : ''}.webp";
    return Image.asset(
      imagePath,
      width: 24,
      height: 24,
    );
  }
}
